package logic

import (
	"context"
	"fmt"
	"taobao-merchandise/models/cache"
	"taobao-merchandise/models/mysql"

	"taobao-merchandise/taobaorpc/internal/svc"
	"taobao-merchandise/taobaorpc/taobaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *taobaorpc.LoginRequest) (*taobaorpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	ChMode := cache.Rdb.Get(l.ctx, "captcha"+in.Account)
	if ChMode.Val() != in.CaptchaCode {
		return nil, fmt.Errorf("图形验证码错误")
	}
	var tb mysql.Ass
	mysql.DB.Debug().Where("account =?", in.Account).Find(&tb)
	if tb.ID == 0 {
		return nil, fmt.Errorf("账号不存在,请前2去注册")
	}
	if in.Password != tb.Password {
		return nil, fmt.Errorf("密码不对")
	}
	return &taobaorpc.LoginResponse{
		UserId: int64(tb.ID),
	}, nil
}
